﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// Qt headers
#include "TranslatorManager.h"
#include <qlabel>
#include <qpushbutton>
#include <qmetaenum>
#include "MessageDialog.h"
#include "GbmpNew.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    MessageDialog::MessageDialog(QWidget *parent, Qt::WindowFlags flags)
        : BaseDialog(parent, flags),
        m_pLblMessage(nullptr),
        m_mpButtons(),
        m_dialogResult(MessageDialogResult::ResultNotSet)
    {
        Init();
    }

    MessageDialog::~MessageDialog()
    {
    }

    void MessageDialog::SetCentralWidget(QWidget * pWidget)
    {
        BaseDialog::SetCentralWidget(pWidget);
    }

    void MessageDialog::AddBottomButton(QPushButton * pButton)
    {
        BaseDialog::AddBottomButton(pButton);
    }

    MessageDialog *MessageDialog::GetInstance(QWidget *parent)
    {
        if (!MessageDialog::s_pInstance)
        {
            MessageDialog::s_pInstance =NEW_AS_QT(MessageDialog,parent);
            DBG_WARN_AND_RETURN_UNLESS(MessageDialog::s_pInstance, MessageDialog::s_pInstance, L"MessageDialog::s_pInstance 为空", L"GMEP", L"2024-01-30");
            MessageDialog::s_pInstance->setObjectName("MessageDialog");
        }
        else if (MessageDialog::s_pInstance->parentWidget() != parent)
        {
            MessageDialog::s_pInstance->deleteLater();
            MessageDialog::s_pInstance = NEW_AS_QT(MessageDialog,parent);
            DBG_WARN_AND_RETURN_UNLESS(MessageDialog::s_pInstance, MessageDialog::s_pInstance, L"MessageDialog::s_pInstance 为空", L"GMEP", L"2024-01-30");
            MessageDialog::s_pInstance->setObjectName("MessageDialog");
        }
        return MessageDialog::s_pInstance;
    }

    void MessageDialog::Init()
    {
        // set MessageDialog self
        SetMinBoxVisible(false);
        SetMaxBoxVisible(false);        

        // set message label
        m_pLblMessage = NEW_AS_QT(QLabel);
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblMessage, L"m_pLblMessage为空", L"GMEP", L"2024-01-30");
        QFont font = m_pLblMessage->font();
        font.setFamily("Microsoft YaHei");
        font.setPixelSize(12);
        m_pLblMessage->setFont(font);
        m_pLblMessage->setAlignment(Qt::AlignCenter);
        m_pLblMessage->setStyleSheet("QLabel{"
            "margin:8px;"
            "color:rgba(40,40,51,255);"
            "}"
        );        
        SetCentralWidget(m_pLblMessage);

        // set dialog buttons
        QString btnSolidStyleSheet(
            "QPushButton{"
            "background-color:rgba(82,145,221,255);"
            "color:rgba(255,255,255,255);"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "border:1px solid rgba(82,145,221,255);"
            "border-radius:2px;"
            "height:24px;"
            "padding-left:8px;"
            "padding-right:8px;"
            "min-width:32px;"
            "}"
            "QPushButton:hover{"
            "background-color:rgba(45,175,205,255);"
            "}"
            "QPushButton:disabled{"
            "background-color:rgba(176, 178, 185, 255);"
            "color:rgba(120, 122, 131, 255);"
            "border:1px solid rgba(176, 178, 185, 255);"
            "}"
        );
        QString btnHollowStyleSheet(
            "QPushButton{"
            "background-color:rgba(255, 255, 255, 255);"
            "color:rgba(82, 145, 221, 255);"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "border:1px solid rgba(82,145,221,255);"
            "border-radius:2px;"
            "height:24px;"
            "padding-left:8px;"
            "padding-right:8px;"
            "min-width:32px;"
            "}"
        );        
        QCursor btnCursor = QCursor(Qt::CursorShape::PointingHandCursor);

        // stretch
        AddBottomStretch();

        // Cancel button
        QPushButton *btnCancel = NEW_AS_QT(QPushButton, QString::fromStdWString(GBMP_TR(L"取消"))); 
        DBG_WARN_AND_RETURN_VOID_UNLESS(btnCancel, L"btnCancel为空", L"GMEP", L"2024-01-30");
        btnCancel->setObjectName("CancelButton");
        btnCancel->setStyleSheet(btnHollowStyleSheet);
        btnCancel->setCursor(btnCursor);
        connect(btnCancel, &QPushButton::clicked, [=]() {
            SetDialogResult(MessageDialogResult::ResultCancel);
            accept();
        });
        AddBottomButton(btnCancel);
        m_mpButtons[MessageDialogButton::BtnCancel] = btnCancel;

        // Ok button
        QPushButton *btnOk = NEW_AS_QT(QPushButton, QString::fromStdWString(GBMP_TR(L"确定"))); 
        DBG_WARN_AND_RETURN_VOID_UNLESS(btnOk, L"btnOk为空", L"GMEP", L"2024-01-30");
        btnOk->setObjectName("OkButton");
        btnOk->setStyleSheet(btnSolidStyleSheet);
        btnOk->setCursor(btnCursor);
        connect(btnOk, &QPushButton::clicked, this, &MessageDialog::OnOk);
        AddBottomButton(btnOk);
        m_mpButtons[MessageDialogButton::BtnOk] = btnOk;
        
    }

    void MessageDialog::OnOk()
    {
        SetDialogResult(MessageDialogResult::ResultOk);
        accept();
    }

    void MessageDialog::SetMessage(QString message)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pLblMessage, L"m_pLblMessage为空", L"GMEP", L"2024-01-30");
        m_pLblMessage->setText(message);
    }

    void MessageDialog::SetButtons(int buttons)
    {
        if (buttons == MessageDialogButton::NoButton)
        {
            for (auto &item : m_mpButtons)
            {
                DBG_WARN_AND_CONTINUE_UNLESS(item, L"item为空", L"GMEP", L"2024-01-30");
                item->setVisible(false);
            }

            return;
        }
        
        if (m_mpButtons.contains(MessageDialogButton::BtnOk))
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_mpButtons[MessageDialogButton::BtnOk], L"m_mpButtons[MessageDialogButton::BtnOk]为空", L"GMEP", L"2024-01-30");
            m_mpButtons[MessageDialogButton::BtnOk]->setVisible((buttons & MessageDialogButton::BtnOk) != 0);
        }

        if (m_mpButtons.contains(MessageDialogButton::BtnCancel))
        {
            DBG_WARN_AND_RETURN_VOID_UNLESS(m_mpButtons[MessageDialogButton::BtnCancel], L"m_mpButtons[MessageDialogButton::BtnCancel]为空", L"GMEP", L"2024-01-30");
            m_mpButtons[MessageDialogButton::BtnCancel]->setVisible((buttons & MessageDialogButton::BtnCancel) != 0);
        }
    }

    void MessageDialog::SetDialogResult(MessageDialogResult result)
    {
        m_dialogResult = result;
    }

    MessageDialog::MessageDialogResult MessageDialog::Show(QWidget *parent, QString title, QString message, int buttons)
    {
        MessageDialog *pDialog = MessageDialog::GetInstance(parent);
        if (!pDialog)
        {
            return MessageDialogResult::ResultNotSet;
        }
        
        pDialog->SetDialogResult(MessageDialogResult::ResultNotSet);

        pDialog->setWindowIcon(QIcon());
        pDialog->setWindowTitle(title);
        pDialog->SetMessage(message);
        pDialog->SetButtons(buttons);
        pDialog->setModal(true);

        pDialog->exec();

        return pDialog->GetDialogResult();
    }

    MessageDialog::MessageDialogResult MessageDialog::GetDialogResult()
    {
        return m_dialogResult;
    }

    QPushButton * MessageDialog::GetButton(MessageDialogButton button)
    {
        if (!m_mpButtons.contains(button))
        {
            return nullptr;
        }

        return m_mpButtons[button];
    }

    MessageDialog *MessageDialog::s_pInstance = nullptr;
}
